package cn.edu.hitwh.yibao.service.impl;

import cn.edu.hitwh.yibao.entity.Disease;
import cn.edu.hitwh.yibao.form.DiseaseForm;
import cn.edu.hitwh.yibao.mapper.DiseaseMapper;
import cn.edu.hitwh.yibao.service.DiseaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import java.util.List;

@Service
public class DiseaseServiceImpl implements DiseaseService {
    @Autowired
    private DiseaseMapper diseaseMapper;

    @Override
    public String select(DiseaseForm diseaseForm, Model model) {
        Disease disease = new Disease();
        BeanUtils.copyProperties(diseaseForm, disease);
        List<Disease> list = diseaseMapper.selectList(disease);
        model.addAttribute("diseaseList", list);
        return "/disease/select";
    }

    @Override
    public String add(DiseaseForm diseaseForm, Model model) {
        Disease disease = new Disease();
        BeanUtils.copyProperties(diseaseForm, disease);
        int count = diseaseMapper.insert(disease);
        if (count > 0) {
            return "redirect:/disease/addComplete";
        } else {
            model.addAttribute("addResult", "添加病种失败！");
            return "/disease/select";
        }
    }

    @Override
    public String update(DiseaseForm diseaseForm, Model model) {
        Disease disease = new Disease();
        BeanUtils.copyProperties(diseaseForm, disease);
        int count = diseaseMapper.update(disease);
        if (count > 0) {
            return "redirect:/disease/updateComplete";
        } else {
            return "/disease/select";
        }
    }

    @Override
    public String delete(String diseaseId, Model model) {
        int count = diseaseMapper.delete(diseaseId);
        if (count > 0) {
            return "redirect:/disease/deleteComplete";
        } else {
            model.addAttribute("deleteResult", "删除病种失败！");
            return "/disease/select";
        }
    }

    @Override
    public String updateInit(String diseaseId, Model model) {
        Disease disease = diseaseMapper.selectById(diseaseId);
        model.addAttribute("disease", disease);
        return "/disease/update";
    }

    @Override
    public String detail(String diseaseId, Model model) {
        Disease disease = diseaseMapper.selectById(diseaseId);
        model.addAttribute("disease", disease);
        return "/disease/detail";
    }
}